<?php

namespace haibao\jiepai\data;


class Advert extends \haibao\jiepai\data\BaseMysql
{

    public function __construct()
    {
        parent::__construct('\haibao\jiepai\model\data\article\CmsArticles');
        $this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_NEW_BI_UTF8);
    }

    public function getAdvertList($adIdArray)
    {
        $adArr = \haibao\jiepai\cache\App::getAdvertList();
        if (!$adArr) {
            $str = preg_replace('/(\d+)/', '%s', implode(',', $adIdArray));
            $sql = 'select a.bannerid,b.interface_banner_id,b.filename,b.bannertext,b.weight,b.zone_id,b.pv_detection,b.url,b.title,c.interface_campaign_id from `banner_acls` as a inner join `banners` as b on a.bannerid=b.bannerid inner join clients as c on b.clientid=c.clientid where a.`data`="' . date('Ymd') . '" and b.zone_id in (' . $str . ') and b.weight=1 and filename!="" order by b.bannerid';
            $result = $this->query($sql, $adIdArray);

            $adArr = array();
            while ($row = $result->fetch_assoc()) {
                $type = (array_search($row['zone_id'], $adIdArray) + 1);
                $row['type'] = $type;
                if ($type == 1) {
                    $row['picture'] = 'http://cdn0.haibao.cn/store/media/dc9868213461574421ab623b7bf9d022.jpg';
                    $row['color'] = \haibao\jiepai\common\ImageColor::imgColor($row['picture']);
                }
                array_push($adArr, $row);
            }
            \haibao\jiepai\cache\App::setAdvertList($adArr, 600);
        }
        return $adArr;
    }

    public function getVideoAdvertList($adIdArray)
    {
        $adArr = \haibao\jiepai\cache\App::getVideoAdvertList();
        if (!$adArr) {
            $str = preg_replace('/(\d+)/', '%s', implode(',', $adIdArray));
            $sql = 'select a.bannerid,b.interface_banner_id,b.filename,b.bannertext,b.weight,b.zone_id,b.pv_detection,b.url,b.title from `banner_acls` as a inner join `banners` as b on a.bannerid = b.bannerid where a.`data` = "' . date('Ymd') . '" and b.zone_id in (' . $str . ') and b.weight = 1 order by b.bannerid';
            $result = $this->query($sql, $adIdArray);

            $adArr = array();
            while ($row = $result->fetch_assoc()) {
                $infoArr = explode('###', $row['bannertext']);
                $row['url'] = $infoArr[0];
                $row['picture'] = $infoArr[1];
                $row['imageUrl'] = $infoArr[2];
                if (isset($infoArr[3])) {
                    $row['floatVideo'] = $infoArr[3];
                }
                $row['type'] = (array_search($row['zone_id'], $adIdArray) + 8);
                unset($row['bannertext']);
                array_push($adArr, $row);
            }
            $adArr && \haibao\jiepai\cache\App::setVideoAdvertList($adArr, 600);
        }
        return $adArr;
    }
}
